﻿using habbo.Habbo.Characters;
using habbo.Kernel.Packets.Interfaces;
using habbo.Kernel.Packets.Messages;
using habbo.Kernel.Storage.Querys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace habbo.Kernel.Packets.com.sulake.habbo.communication.messages.incoming.handshake
{
    class AuthenticationOKEvent : IMessageEvent
    {
        public void Invoke(Network.Session Session, Messages.ClientPacket Packet)
        {
            string SSOTicket = Packet.PopFixedString();

            Character Obj = SystemApp.CharacterManager.Authenticate(SSOTicket);

            if (Obj != null)
            {
                Session.UpdateCharacter(Obj);
                ServerPacket packet = new ServerPacket(Headers.MessageComposerIds.AuthenticationOKMessageComposer);
                Session.Send(packet);
                SystemApp.MySQLManager.GetObject(new CharacterSetLastLogin(Obj.Id, SystemApp.GetUnixTimestamp()));
            }
            else
            {
                ServerPacket packet = new ServerPacket(Headers.MessageComposerIds.NotificationBroadcastMessageComposer);
                packet.Append("Your SSO ticket was wrong. Please, try again.");
                packet.Append("");
                Session.Send(packet);

                System.Threading.Thread.Sleep(5000);

                Session.Socket.Disconnect(false);
                Session.Socket.Dispose();
                Session.OnConnectionClose();
            }
        }
    }
}
